package com.qf.admin.dao.impl;


import com.qf.admin.dao.ISys_UserDao;
import com.qf.admin.domain.Sys_User;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class Sys_UserDaoImpl implements ISys_UserDao {

    QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

    @Override
    public void addGetSys_UserData(String sname, String password) {
        String addSys_userSql = "insert into sys_user(sname,password)values(?,?)";
        try {
            queryRunner.update(addSys_userSql,sname,password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }

    @Override
    public void deleteSys_UserData(int id) {
        String deleteSys_userSql ="delete from sys_user where id=?";
        try {
            queryRunner.update(deleteSys_userSql,id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    public List<Sys_User> getSys_User() {
        String getAllSys_userSql = "select id,sname,password from sys_user";
        try {
            List<Sys_User> sys_users = queryRunner.query(getAllSys_userSql, new BeanListHandler<Sys_User>(Sys_User.class));
            return sys_users;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Sys_User checkSys_UserNamePassword(String userName, String password) {

        String sql="select * from sys_user where sname=? and password=? ";
        try {
            Sys_User sysUser = queryRunner.query(sql, new BeanHandler<>(Sys_User.class), userName, password);
            return  sysUser;
        } catch (SQLException throwables) {

            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public Sys_User getUser(int userId) {
        String sql="select id,sname,password from sys_user where id=?";

        try {
            return queryRunner.query(sql,new BeanHandler<>(Sys_User.class),userId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
